package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.CommentTag;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.CommentTagMapper;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private ITagService tagService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private CommentTagMapper commentTagMapper;
    @GetMapping("/tags")
    public Object getTags(){
        return tagService.getTags();
    }
    @PostMapping("/tags")
    public Object createComement(Comment entity,Long[] tags,String myTags){
        entity.setReleaseTime(new Date());
        entity.setPicUrl(entity.getPicUrl().substring(0,entity.getPicUrl().length()-1));
        entity.setUserId(UserContext.getCurrentUser().getId());
        commentService.saveOrUpdate(entity);
        //自定义标签的数组
        String[] mytagArr = myTags.split(" ");
        for (String s : mytagArr) {
            //插入自定义地标签
            Tag tag = new Tag();
            tag.setContent(s);
            tag.setState(false);
            tag.setStrategyId(entity.getStrategyId());
            tagService.saveOrUpdatePraise(tag);
            //保存自定义点评与标签的关系
            CommentTag commentTag = new CommentTag();
            commentTag.setCommentId(entity.getId());
            commentTag.setTagId(tag.getId());
            commentTagMapper.insert(commentTag);
        }
        //保存点评与标签的关系
        for (Long tagId : tags) {
            CommentTag commentTag = new CommentTag();
            commentTag.setCommentId(entity.getId());
            commentTag.setTagId(tagId);
            commentTagMapper.insert(commentTag);
        }
        return new JSONResult();
    }

    @GetMapping
    public Object getComments(CommentQueryObject qo,String myType){
        if (myType==null) {
            qo.setOrderBy("c.releaseTime desc");
            PageInfo<Comment> pageInfo = commentService.query(qo);
            for (Comment comment : pageInfo.getList()) {
                String[] split = comment.getPicUrl().split(",");
                ArrayList<Object> picList = new ArrayList<>();
                for (String s : split) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("pic", s);
                    picList.add(map);
                }
                comment.setSinglePic(picList);
            }
            return pageInfo;
        }
        qo.setOrderBy("c.releaseTime desc");
        qo.setPageSize(0);
        return commentService.query(qo);
    }

    @GetMapping("/{id}")
    public Object get(@PathVariable Long id){
        Comment comment = commentService.getComment(id);
        String[] split = comment.getPicUrl().split(",");
        ArrayList<Object> picList = new ArrayList<>();
        for (String s : split) {
            HashMap<String, String> map = new HashMap<>();
            map.put("pic",s);
            picList.add(map);
        }
        comment.setSinglePic(picList);
        return comment;
    }
    @GetMapping("/{id}/tags")
    public Object getTag(@PathVariable Long id){
        return commentService.getTagById(id);
    }
}
